package com.cnoja.jmsncn.ui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.cnoja.jmsncn.command.SocketCommander;
import com.cnoja.jmsncn.kernel.DefaultMessengerImpl;
import com.cnoja.jmsncn.ui.Shell;
import com.cnoja.jmsncn.utils.text.StringUtils;

public class LoginAction extends AbstractAction{

	private Shell shell=null;
	public LoginAction(Shell shell){
		this.putValue(NAME, "登录");
		this.shell=shell;
	}
	
	public void actionPerformed(ActionEvent e) {
		JTextField userName=(JTextField)shell.getComponentByName("userName");
		JPasswordField password=(JPasswordField)shell.getComponentByName("password");
		String userNameString=userName.getText();
		String passwordString=new String(password.getPassword());
		if(StringUtils.isNullOrEmpty(userNameString)){
			JOptionPane.showMessageDialog(shell.getContentPane(), "请输入用户名");
			userName.requestFocus();
			return;
		}
		if(StringUtils.isNullOrEmpty(passwordString)){
			JOptionPane.showMessageDialog(shell.getContentPane(), "请输入密码");
			password.requestFocus();
			return;
		}
		
		DefaultMessengerImpl defaultMessengerImpl = new DefaultMessengerImpl(userNameString,passwordString);
		defaultMessengerImpl.setHost("messenger.hotmail.com");
		defaultMessengerImpl.setPort(1863);
		defaultMessengerImpl.setCommander(new SocketCommander());
		shell.setMessenger(defaultMessengerImpl);
		try{
		defaultMessengerImpl.login();
		}catch(Exception ex){
			JOptionPane.showMessageDialog(this.shell, ex.getMessage());
			ex.printStackTrace();
		}
		
		
		JButton button = (JButton)e.getSource();
		
//		button.setEnabled(false);
	}

}
